package com.manyToOne.service;

import com.manyToOne.model.Customer;
import com.manyToOne.model.Order;
import com.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import java.util.Iterator;
import java.util.List;

public class BusinessService {

    private SessionFactory sessionFactory = new HibernateUtil().getSessionFactory();

    private List findOrdersByCustomer(Customer customer) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            List orders = session.createQuery("from Order as o where o.customer.id=" + customer.getId())
                    .list();
            tx.commit();
            return orders;
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private Customer findCustomer(long customer_id) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Customer customer = (Customer) session.get(Customer.class, new Long(customer_id));
            tx.commit();
            return customer;
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
                System.out.println();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private void saveCustomerAndOrderWithCascade() {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            Customer customer = new Customer("Jack");
            Order order1 = new Order("Jack_Order001", customer);
            Order order2 = new Order("Jack_Order002", customer);

            session.save(order1);
            session.save(order2);

            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

    private void saveCustomerAndOrder() {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Customer customer = new Customer("Tom");
            session.save(customer);
            Order order1 = new Order("Tom_Order001", customer);
            Order order2 = new Order("Tom_Order002", customer);
            session.save(order1);
            session.save(order2);
            tx.commit();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private void printOrders(List orders) {
        for (Iterator it = orders.iterator(); it.hasNext(); ) {
            Order order = (Order) it.next();
            System.out.println("OrderNumber of " + order.getCustomer().getName() + " :" + order.getOrderNumber());
        }
    }

    private void test() {
        saveCustomerAndOrder();
        saveCustomerAndOrderWithCascade();
        Customer customer = findCustomer(1);
        List orders = findOrdersByCustomer(customer);
        printOrders(orders);
        sessionFactory.close();
    }

    public static void main(String args[]) {
        new BusinessService().test();
    }
}




